<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>扫码登录确认</title>
    <script src="https://unpkg.com/axios@1.5.0/dist/axios.min.js"></script>
    <style>
        #info {
            height: 400px;
            line-height: 400px;
            font-size: 20px;
            padding: 20px;
        }

        #confirm,
        #cancel {
            display: block;
            width: 80%;
            line-height: 40px;
            font-size: 20px;
            margin-bottom: 20px;
        }

        #confirm {
            background: skyblue;
        }
    </style>
</head>

<body>
    <button id="newking">登录newking账号</button>
    <button id="newking123">登录newking123账号</button>

    <div id="info">
        是否确认登录 xxx 网站？ <span id="info"></span>
    </div>
    <button id="confirm">确认登录</button>
    <button id="cancel">取消</button>

    <script>
        // console.log(window.location.search); 
        // >> ?id=d4b2eae6-46c6-4344-94aa-ec36c5cfdc1c%22
        const params = new URLSearchParams(window.location.search.slice(1));
        const id = params.get('id');
        document.getElementById('info').textContent = id;

        let token = '';
        newking.addEventListener('click', () => {
            axios.get('http://192.168.0.221:3000/login', {
                params: {
                    username: 'newking',
                    password: 'zjhQQsybzd'
                }
            }).then(res => {
                token = res.data.token;
            });
        });

        newking123.addEventListener('click', () => {
            axios.get('http://192.168.0.221:3000/login', {
                params: {
                    username: 'newking123',
                    password: 'zjhQQsybzd'
                }
            }).then(res => {
                token = res.data.token;
            });
        });

        //修改状态: scan-wait-confirm
        axios.get('http://192.168.0.221:3000/qrcode/scan?id=' + id).catch(e => {
            alert('二维码已过期');
        });


        //修改状态: confirm
        document.getElementById('confirm').addEventListener('click', () => {
            axios.get('http://192.168.0.221:3000/qrcode/confirm?id=' + id, {
                headers: {
                    authorization: 'Bearer ' + token
                }
            }).then(res => {
                alert(res.data.userInfo.username)
            }).catch(e => {
                alert(e.response.data.message);
            });
        });


        //修改状态: cancel
        document.getElementById('cancel').addEventListener('click', () => {
            axios.get('http://192.168.0.221:3000/qrcode/cancel?id=' + id).catch(e => {
                alert('二维码已过期');
            });
        });
    </script>
</body>

</html>